import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ClientsModule, Transport } from '@nestjs/microservices'; 
import { SequelizeModule } from '@nestjs/sequelize';
import { UserModule } from './user/user.module';
import { UserService } from './user/user.service'; 
import { AuthModule } from './auth/auth.module';
import { AuthGuard } from './auth/auth.guard';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import FormatResponseInterceptor from '@libs/common/interceptor/format.response';
import { AllExceptionsFilter } from '@libs/common/filter/exception';
import { JwtModule } from '@nestjs/jwt';
import { ConfigModule } from '@nestjs/config';
import load from '@libs/common/config'; 


@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [AppModule.loadYaml]
    }),
    JwtModule.registerAsync({
      global: true,
      async useFactory(){
        const ret = AppModule.loadYaml()
        return ret.jwt
      },
    }),
    ClientsModule.register([{
      name: 'COMMON_SERVICE',
      transport: Transport.TCP,
      options: {
        port: 8888
      }}]),
    SequelizeModule.forRootAsync({
        async useFactory() {
          const ret = AppModule.loadYaml()
          return ret.db.mysql
        }
      }),
    UserModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService, {
    provide: APP_GUARD,
    useClass: AuthGuard,
  }, {
    provide: APP_INTERCEPTOR,
    useClass: FormatResponseInterceptor,
  }
  ,{
    provide: APP_FILTER,
    useClass: AllExceptionsFilter,
  },
],
  
})
export class AppModule {
  static loadYaml(){
    return load(__dirname)()
  }
}
